package com.beemans.common.app.ui.fragments

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import com.beemans.common.app.R
import com.beemans.common.app.databinding.FragmentStickyBinding
import com.beemans.common.app.ui.base.BaseFragment
import com.beemans.common.ext.setPageBack
import com.beemans.common.ui.adapter.CustomFragmentStateAdapter
import com.beemans.common.ui.views.MyNestedScrollView
import com.tiamosu.databinding.delegate.lazyDataBindingOrNull
import com.tiamosu.databinding.page.DataBindingConfig
import com.tiamosu.fly.ext.clickNoRepeat

/**
 * @author tiamosu
 * @date 2021/8/30.
 */
class StickyFragment : BaseFragment() {
    private val dataBinding: FragmentStickyBinding? by lazyDataBindingOrNull { }

    private var title: String? = null

    override fun getDataBindingConfig(): DataBindingConfig {
        return DataBindingConfig(R.layout.fragment_sticky)
    }

    override fun initParameters(bundle: Bundle?) {
        title = getString(MainFragment.PARAM_TITLE)
    }

    override fun initView(rootView: View?) {
        dataBinding?.stickyTitleBar?.setTvTitle { text = title }
    }

    override fun onFlyLazyInitView() {
        super.onFlyLazyInitView()

        val fragments = mutableListOf<Fragment>().apply {
            add(StickyChildFragment())
            add(StickyChildFragment())
        }
        dataBinding?.stickyVp?.let { CustomFragmentStateAdapter(this, it, fragments) }

        postDelayed({
            showToast("可永久吸顶啦~")
            dataBinding?.stickySv?.isStickyMode = true
        }, 2000)
    }

    override fun initEvent() {
        dataBinding?.stickyTitleBar?.setPageBack()

        dataBinding?.stickySv?.setStickyFixedListener { isStickyFixed ->
            Log.e(
                "susu",
                "isStickyFixed:$isStickyFixed   ${dataBinding?.stickySv?.isStickyFixed()}"
            )

            if (isStickyFixed) {
                postDelayed({
                    Log.e("susu", "zzzzzzzzzzzzz")
                    dataBinding?.stickyTvTest?.isVisible = true
                }, 5000)
            }
        }

        dataBinding?.stickySv?.setScrollListener(object :
            MyNestedScrollView.DefaultScrollListener() {
            override fun onScrollStateChanged(newState: Int) {
//                val state = when (newState) {
//                    MyNestedScrollView.SCROLL_STATE_IDLE -> "停止滑动"
//                    else -> "滑动中"
//                }
//                Log.e("susu", "onScrollStateChanged:$state")
            }

            override fun onScrollToStart() {
//                Log.e("susu", "onScrollToStart")
            }

            override fun onScrollToEnd() {
//                Log.e("susu", "onScrollToEnd")
            }
        })

        dataBinding?.stickyTvUnStickyFixed?.clickNoRepeat {
            dataBinding?.stickySv?.unStickyFixed()
        }

        dataBinding?.stickyTvStickyFixed?.clickNoRepeat {
            dataBinding?.stickySv?.stickyFixed()
        }
    }

    override fun doBusiness() {
    }
}